﻿using System.ComponentModel;
using System.Web.UI;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Shweet.Engine;
using Shweet.Repository;
using System.Web.UI.WebControls.WebParts;
using Teams = Shweet.Repository.Teams;

namespace Shweet.SPSolution.ShweetReader
{
    [XmlRoot(Namespace = "Shweet")]
    public class ShweetReader : System.Web.UI.WebControls.WebParts.WebPart
    {
        public const string DEFAULT_TAG = "";
        private string _tag01 = "";
        private string _tag02 = "";
        private string _tag03 = "";
        private string _tag04 = "";
        private string _tag05 = "";

        #region "WebPart Properties"
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(DEFAULT_TAG),
        WebPartStorage(Storage.Shared),
        FriendlyName("Search Tag 1"), Description("Search Tag 1"),
        SPWebCategoryName("Shweet")]
        public string Tag01
        {
            get { return _tag01; }
            set { _tag01 = value;}
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(DEFAULT_TAG),
        WebPartStorage(Storage.Shared),
        FriendlyName("Search Tag 2"), Description("Search Tag 2"),
        SPWebCategoryName("Shweet")]
        public string Tag02
        {
            get { return _tag02; }
            set { _tag02 = value; }
        }
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(DEFAULT_TAG),
        WebPartStorage(Storage.Shared),
        FriendlyName("Search Tag 3"), Description("Search Tag 3"),
        SPWebCategoryName("Shweet")]
        public string Tag03
        {
            get { return _tag03; }
            set { _tag03 = value; }
        }
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(DEFAULT_TAG),
        WebPartStorage(Storage.Shared),
        FriendlyName("Search Tag 4"), Description("Search Tag 4"),
        SPWebCategoryName("Shweet")]
        public string Tag04
        {
            get { return _tag04; }
            set { _tag04 = value; }
        }
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(DEFAULT_TAG),
        WebPartStorage(Storage.Shared),
        FriendlyName("Search Tag 5"), Description("Search Tag 5"),
        SPWebCategoryName("Shweet")]
        public string Tag05
        {
            get { return _tag05; }
            set { _tag05 = value; }
        }
        #endregion 

        protected override void RenderContents(HtmlTextWriter writer)
        {
            IShweetMessages messages = new ShweetMessages();
            ITeams teamsFollowing = new Teams();
            Reader shweetEngine = new Reader(messages, teamsFollowing);

            string requestTag = Page.Request.QueryString["tag"];

            string [] tags = new string[0];
            if (string.IsNullOrEmpty(requestTag))
            {
                if (!string.IsNullOrEmpty(string.Concat(_tag01, _tag02, _tag03, _tag04, _tag05)))
                {
                    tags = new string[] { _tag01, _tag02, _tag03, _tag04, _tag05 };
                }                
            }
            else
            {
                tags = new string[] {string.Format("{0}",requestTag), "","","",""};
            }

                
            Shweet.Repository.Shweet [] shweetMessages = shweetEngine.Read(tags);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderBeginTag(HtmlTextWriterTag.P);
            foreach (Shweet.Repository.Shweet currentShweet in shweetMessages)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                writer.WriteLine(Parser.ConvertTagsToLinks(currentShweet.Text, currentShweet.Tags));
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }
}